{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def split_csv(path, total_len, per):\n",
    "    read_path = 'D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem'\n",
    "    save_path = 'D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem\\\\train_m.csv'\n",
    "    # 如果保存路径存在就删除, 重新开始\n",
    "    if os.path.exists('D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem\\\\new_train.csv'):\n",
    "        os.remove('D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem\\\\new_train.csv')\n",
    "    with open(path, 'r', newline='') as file:\n",
    "        csvreader = csv.reader(file)\n",
    "        i = 0\n",
    "        for row in csvreader:\n",
    "            if i < round(total_len * per/100):\n",
    "                # train.csv存放路径\n",
    "                csv_path = os.path.join(\"D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem\", 'new_train.csv')\n",
    "                print(csv_path)\n",
    "                # 不存在此文件的时候，就创建\n",
    "                if not os.path.exists(csv_path):\n",
    "                    with open(csv_path, 'w', newline='') as file:\n",
    "                        csvwriter = csv.writer(file)\n",
    "                        csvwriter.writerow(row)\n",
    "                    i += 1\n",
    "                # 存在的时候就往里面添加\n",
    "                else:\n",
    "                    with open(csv_path, 'a', newline='') as file:\n",
    "                        csvwriter = csv.writer(file)\n",
    "                        csvwriter.writerow(row)\n",
    "                    i += 1\n",
    "            elif(True):\n",
    "                break\n",
    "            elif (i >= round(total_len * per/100)) and (i < total_len):\n",
    "            \t# vali.csv存放路径\n",
    "                csv_path = os.path.join(\"D:\\\\文档\\\\URP\\\\毕业设计\\\\Data\\\\Train\\\\uspto_mol\", 'vali.csv')\n",
    "                print(csv_path)\n",
    "                # 不存在此文件的时候，就创建\n",
    "                if not os.path.exists(csv_path):\n",
    "                    with open(csv_path, 'w', newline='') as file:\n",
    "                        csvwriter = csv.writer(file)\n",
    "                        csvwriter.writerow(row)\n",
    "                    i += 1\n",
    "                # 存在的时候就往里面添加\n",
    "                else:\n",
    "                    with open(csv_path, 'a', newline='') as file:\n",
    "                        csvwriter = csv.writer(file)\n",
    "                        csvwriter.writerow(row)\n",
    "                    i += 1\n",
    "            else:\n",
    "                break\n",
    "\n",
    "    print(\"训练集和验证集分离成功\")\n",
    "    return\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "if __name__ == '__main__':\n",
    "\n",
    "    path = 'D:\\\\Data\\\\Train\\\\pubchem\\\\pubchem\\\\train_1m.csv'\n",
    "    total_len = len(open(path, 'r').readlines())# csv文件行数\n",
    "    per = 1 # 分割比例%\n",
    "\n",
    "    split_csv(path, total_len, per)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "molscribe",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
